<?php
/*
*订单接口类
*/
class orderAppClass extends appBase{
	/*查询条件*/
	public function search(){
		if(!$this->power("order","seeAll")){
			$stid = "是";
		}else{
			$stid = "否";
		}
		$json['search'] = array(
			"workFlow" => array("草稿","审核中","已通过","开发中","已完成"),//订单状态
			"stid" => $stid,//查询全部员工
			"orderBy" => array("合同金额升序","合同金额降序","费用计提降序","立项日升序","立项日降序","交付日升序","交付日降序"),//排序
		);
		//返回
		$json['warn'] = "success";
		return $json;
	}
	/*订单列表*/
	public function home(){
		if(!$this->power("order","see")){
			$json['warn'] = "您没有查看订单的权限";
		}else{
			//like查询数组
			$like = array(
				"name" => $this->post['name'],//订单名称
			);
			//全等查询数组
			$equal = array(
				"stid" => $this->post['stid'],//所属员工
				"workFlow" => $this->post['workFlow'],//订单状态
			);
			//订单状态
			$and = "";
			//如果没有查看所有员工订单的权限
			if(!$this->power("order","seeAll")){
				$and .= " and stid = '".$this->control['stid']."'";
			}
			//生成查询语句
			$where = search(array("like" => $like,"=" => $equal)).$and;
			//排序
			$orderBy = $this->orderBy(array(
				"search" => $this->post['orderBy'],
				"default" => "立项日降序",
				"array" => array(
					"money" => array("asc" => "合同金额升序","desc" => "合同金额降序"),
					"cost" => array("desc" => "费用计提降序"),
					"signDay" => array("asc" => "立项日升序","desc" => "立项日降序"),
					"endDay" => array("asc" => "交付日升序","desc" => "交付日降序"),
				),
			));
			//获取查询结果
			$page = $this->page(array("table" => "buyCar","where" => $where,"order" => $orderBy['orderBy']));
			//数据分析
			$json['order'] = array();
			foreach($page['array'] as $array){
				//负责人
				$staff = query("staff","stid = '$array[stid]'");
				//合同净值
				$netWorthOrder = $array['money'] - $array['cost'];
				//合同应收
				$moneyWait = $array['money'] - $array['moneyIn'];
				//净回款
				$netWorthIn = $array['moneyIn'] - $array['moneyOut'];
				//剩余净值
				$netWorthWait = $netWorthOrder - $netWorthIn;
				//预计奖金
				$buyCarAllotMoney = find(array("table" => "buyCarAllot","sum" => "money","where" => " buyCarId = '$array[id]' "));
				//拨付比例
				if($netWorthOrder == 0){
					$ratio = 0;
				}else{
					$ratio = round($buyCarAllotMoney/$netWorthOrder*100,2);
				}
				//已拨付
				$buyCarAllotPay = find(array("table" => "buyCarAllot","sum" => "pay","where" => " buyCarId = '$array[id]' "));
				//奖金余额
				$buyCarAllotSurplus = $buyCarAllotMoney - $buyCarAllotPay;
				//返回
				$json['order'][] = array(
					"id" => $array['id'],//订单号
					"workFlow" => $array['workFlow'],//状态
					"name" => $array['name'],//订单名称
					"money" => $array['money'],//合同金额
					"staffName" => $staff['name'],//负责人
					"cost" => $array['cost'],//费用计提
					"netWorthOrder" => $netWorthOrder,//合同净值
					"moneyWait" => $moneyWait,//合同应收
					"netWorthIn" => $netWorthIn,//净回款
					"netWorthWait" => $netWorthWait,//剩余净值
					"ratio" => $ratio,//拨付比例
					"buyCarAllotMoney" => $buyCarAllotMoney,//预计奖金
					"buyCarAllotPay" => $buyCarAllotPay,//已拨付
					"buyCarAllotSurplus" => $buyCarAllotSurplus,//奖金余额
				);
			}
			//分页信息
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*订单详情页判断*/
	public function detailPower($orderId){
		$buyCar = query("buyCar","id = '$orderId'");
		if(empty($orderId)){
			$json['warn'] = "订单ID为空";
		}elseif(empty($buyCar['id'])){
			$json['warn'] = "未找到此订单";
		}elseif($buyCar['stid'] != $this->control['stid'] and !$this->power("order","seeAll")){
			$json['warn'] = "这不是您的订单，且没有查看所有订单的权限";
		}else{
			$json = $buyCar;
		}
		return $json;
	}
	/*订单详情*/
	public function detail(){
		//准备数据
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//合同净值
			$netWorthOrder = $buyCar['money'] - $buyCar['cost'];
			//合同应收
			$moneyWait = $buyCar['money'] - $buyCar['moneyIn'];
			//净回款
			$netWorthIn = $buyCar['moneyIn'] - $buyCar['moneyOut'];
			//剩余净值
			$netWorthWait = $netWorthOrder - $netWorthIn;
			//工期
			$cycle = (strtotime($buyCar['endDay']) - strtotime($buyCar['signDay'])) / 86400;
			//订单基本参数
			$json['order'] = array(
				"id" => $buyCar['id'],//订单号
				"name" => $buyCar['name'],//订单名称
				"money" => $buyCar['money'],//合同金额
				"cost" => $buyCar['cost'],//费用计提
				"netWorthOrder" => $netWorthOrder,//合同净值
				"moneyIn" => $buyCar['moneyIn'],//已回款
				"moneyOut" => $buyCar['moneyOut'],//已付费用
				"netWorthIn" => $netWorthIn,//净回款
				"moneyWait" => $moneyWait,//合同应收
				"netWorthWait" => $netWorthWait,//剩余净值
				"workFlow" => $buyCar['workFlow'],//订单状态
				"signDay" => $buyCar['signDay'],//立项日期
				"endDay" => $buyCar['endDay'],//交付日期
				"cycle" => $cycle,//工期
			);
			//是否可编辑
			if($this->power("order","edit") and $this->control['stid'] == $buyCar['stid'] and $buyCar['workFlow'] == "草稿"){
				$json['order']['edit'] == "是";
			}else{
				$json['order']['edit'] == "否";
			}
			//订单附件
			$fileSql = find(array("table" => "file","where" => "target = '订单' and targetId = '$buyCar[id]' order by time desc limit 2"));
			$json['file'] = array();
			foreach($fileSql['array'] as $array){
				$fileStaff = query("staff","stid = '$array[stid]'");
				$json['file'][] = array(
					"staffName" => $fileStaff['name'],//上传人
					"name" => $array['name'],//附件名称
					"secret" => $array['secret'],//保密
					"format" => $array['format'],//文件格式
					"size" => $array['size'],//体积MB
					"src" => root.$array['src'],//路径
					"time" => $array['time'],//上传时间
				);
			}
			//费用计提
			$json['cost'] = array();
			$buyCarCostSql = find(array("table" => "buyCarCost","where" => "buyCarId = '$buyCar[id]' order by time limit 2"));
			foreach($buyCarCostSql['array'] as $array){
				$json['cost'][] = array(
					"money" => $array['money'],//计提金额
					"text" => $array['text'],//计提备注
				);
			}
			//跟进记录
			$json['follow'] = array();
			$followSql = find(array("table" => "follow","where" => "target = '订单' and targetId = '$buyCar[id]' order by time limit 2"));
			foreach($followSql['array'] as $array){
				$followStaff = query("staff","stid = '$array[stid]'");
				if($this->power("order","delFollow") and $array['stid'] == $this->control['stid']){
					$del = "是";
				}else{
					$del = "否";
				}
				$json['follow'][] = array(
					"id" => $array['id'],//跟进ID
					"text" => $array['text'],//跟进内容
					"staffName" => $followStaff['name'],//跟进人
					"time" => $array['time'],//跟进时间
					"del" => $del,//删除（是/否）
				);
			}
			//任务分配
			$json['allot'] = array();
			$allotSql = find(array("table" => "buyCarAllot","where" => "buyCarId = '$buyCar[id]' order by time limit 2"));
			foreach($allotSql['array'] as $array){
				//负责人
				$allotStaff = query("staff","stid = '$array[stid]'");
				//拨付比例
				if(empty($array['money'])){
					$rate = 0;
				}else{
					$rate = round($array['money'] / $buyCar['money'],2);
				}
				//奖金余额
				$balance = $array['money'] - $array['pay'];
				//返回
				$json['allot'][] = array(
					"name" => $array['name'],//任务名称
					"staffName" => $allotStaff['name'],//负责人
					"edit" => $array['editPower'],//编辑权
					"rate" => $rate."%",//拨付比例
					"money" => $array['money'],//预计奖金
					"pay" => $array['pay'],//已拨付
					"balance" => $balance,//奖金余额
					"summary" => $array['summary'],//任务介绍
				);
			}
			//回款记录
			$json['pay'] = array();
			$paySql = find(array("table" => "buyCarPay","where" => "buyCarId = '$buyCar[id]' order by time desc limit 2"));
			foreach($paySql['array'] as $array){
				$profit = query("profit","id = '$array[profitId]'");
				$json['pay'][] = array(
					"money" => $array['money'],//回款金额
					"text" => $profit['text'],//回款备注
					"payDate" => $profit['payDate'],//回款日期
				);
			}
			//已付费用
			$json['out'] = array();
			$outSql = find(array("table" => "profit","where" => "target = '订单已付费用' and targetId = '$buyCar[id]' order by payDate desc limit 2"));
			foreach($outSql['array'] as $array){
				$json['out'][] = array(
					"type" => $array['type'],//类型
					"money" => $array['money'],//金额
					"text" => $array['text'],//备注
					"payDate" => $array['payDate'],//日期
				);
			}
			//提示信息
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*审核记录*/
	public function auditingFollow(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			$sql = find(array("table" => "auditingFollow","where" => "type = '订单' and typeId = '".$this->post['id']."' order by time desc "));
			$json['follow'] = array();
			foreach($sql['array'] as $array){
				$json['follow'][] = array(
					"text" => $array['text'],//审核记录
					"time" => $array['time'],//审核时间
				);
			}
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*订单附件列表*/
	public function orderFile(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//数据查询
			if($this->get['type'] == "more"){
				$num = 10;
			}else{
				$num = 2;
			}
			$where = "target = '订单' and targetId = '$buyCar[id]'";
			$page = $this->page(array("table" => "file","where" => $where,"order" => "time","num" => $num));
			//数据分析
			$json['file'] = array();
			foreach($page['array'] as $array){
				$staff = query("staff","stid = '$array[stid]'");
				$json['file'][] = array(
					"name" => $array['name'],//附件名称
					"staffName" => $staff['name'],//上传人
					"secret" => $array['secret'],//保密
					"format" => $array['format'],//格式
					"size" => $array['size'],//体积
					"src" => $array['src'],//地址
					"time" => $array['time'],//上传时间
				);
			}
			//分页
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*费用计提*/
	public function cost(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//数据查询
			if($this->get['type'] == "more"){
				$num = 10;
			}else{
				$num = 2;
			}
			$where = "buyCarId = '$buyCar[id]'";
			$page = $this->page(array("table" => "buyCarCost","where" => $where,"order" => "time","num" => $num));
			//数据分析
			$json['cost'] = array();
			foreach($page['array'] as $array){
				$json['cost'][] = array(
					"money" => $array['money'],//计提金额
					"text" => $array['text'],//计提备注
				);
			}
			//分页
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*跟进记录*/
	public function follow(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//数据查询
			if($this->get['type'] == "more"){
				$num = 10;
			}else{
				$num = 2;
			}
			$where = "target = '订单' and targetId = '$buyCar[id]'";
			$page = $this->page(array("table" => "follow","where" => $where,"order" => "time","num" => $num));
			//数据分析
			$json['follow'] = array();
			foreach($page['array'] as $array){
				$followStaff = query("staff","stid = '$array[stid]'");
				if($this->power("order","delFollow") and $array['stid'] == $this->control['stid']){
					$del = "是";
				}else{
					$del = "否";
				}
				$json['follow'][] = array(
					"id" => $array['id'],//跟进ID
					"text" => "text",//跟进内容
					"staffName" => $followStaff['name'],//跟进人
					"time" => $array['time'],//跟进时间
					"del" => $del,//删除（是/否）
				);
			}
			//分页
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*任务分配*/
	public function allot(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//数据查询
			if($this->get['type'] == "more"){
				$num = 10;
			}else{
				$num = 2;
			}
			$where = "buyCarId = '$buyCar[id]'";
			$page = $this->page(array("table" => "buyCarAllot","where" => $where,"order" => "time","num" => $num));
			//数据分析
			$json['allot'] = array();
			foreach($page['array'] as $array){
				//负责人
				$allotStaff = query("staff","stid = '$array[stid]'");
				//拨付比例
				if(empty($array['money'])){
					$rate = 0;
				}else{
					$rate = round($array['money'] / $buyCar['money'],2);
				}
				//奖金余额
				$balance = $array['money'] - $array['pay'];
				//返回
				$json['allot'][] = array(
					"name" => $array['name'],//任务名称
					"staffName" => $allotStaff['name'],//负责人
					"edit" => $array['editPower'],//编辑权
					"rate" => $rate."%",//拨付比例
					"money" => $array['money'],//预计奖金
					"pay" => $array['pay'],//已拨付
					"balance" => $balance,//奖金余额
					"summary" => $array['summary'],//任务介绍
				);
				//分页
				$json['page'] = $page['para'];
				//提示
				$json['warn'] = "success";
			}
		}
		//返回
		return $json;
	}
	/*回款记录*/
	public function pay(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//数据查询
			if($this->get['type'] == "more"){
				$num = 10;
			}else{
				$num = 2;
			}
			$where = "buyCarId = '$buyCar[id]'";
			$page = $this->page(array("table" => "buyCarPay","where" => $where,"order" => "time desc","num" => $num));
			//数据分析
			$json['pay'] = array();
			foreach($page['array'] as $array){
				$profit = query("profit","id = '$array[profitId]'");
				$json['pay'][] = array(
					"money" => $array['money'],//回款金额
					"text" => $profit['text'],//回款备注
					"payDate" => $profit['payDate'],//回款日期
				);
			}
			//分页
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*已付费用*/
	public function out(){
		$buyCar = $this->detailPower($this->post['id']);
		if(!empty($buyCar['warn'])){
			$json['warn'] = $buyCar['warn'];
		}else{
			//数据查询
			if($this->get['type'] == "more"){
				$num = 10;
			}else{
				$num = 2;
			}
			$where = "target = '订单已付费用' and targetId = '$buyCar[id]'";
			$page = $this->page(array("table" => "profit","where" => $where,"order" => "payDate desc","num" => $num));
			//数据分析
			$json['out'] = array();
			foreach($page['array'] as $array){
				$json['out'][] = array(
					"type" => $array['type'],//类型
					"money" => $array['money'],//金额
					"text" => $array['text'],//备注
					"payDate" => $array['payDate'],//日期
				);
			}
			//分页
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*根据客户名称关键词获得客户ID*/
	public function clientKey(){
		if(empty($this->post['key'])){
			$json['warn'] = "关键词为空";
		}else{
			$json['kehu'] = array();
			$sql = find(array("table" => "kehu","where" => "stid = '".$this->control['stid']."' and companyName like '%".$this->post['key']."%'"));
			foreach($sql['array'] as $array){
				$json['kehu'][] = array(
					"khid" => $array['khid'],
					"companyName" => $array['companyName'],
				);
			}
			$json['warn'] = "success";
		}
		return $json;
	}
	/*订单基本信息编辑*/
	public function edit(){
		//赋值
		$field = array(
			"name" => $this->post['name'],//订单名称
			"money" => $this->post['money'],//合同金额
			"text" => $this->post['text'],//备注
			"updateTime" => date("Y-m-d H:i:s"),//更新时间
		);
		if(empty($this->post['khid'])){
			$buyCar = query("buyCar"," id = '".$this->post['id']."' ");
			$kehu = query("kehu"," khid = '$buyCar[khid]' ");
		}else{
			$kehu = query("kehu"," khid = '".$this->post['khid']."' ");
		}
		//判断
		if(!$this->power("order","edit")){
			$json['warn'] = "您没有编辑订单的权限";
		}elseif(empty($kehu['khid'])){
			$json['warn'] = "未找到此客户";
		}elseif($kehu['stid'] != $this->control['stid']){
			$json['warn'] = "这不是您的客户";
		}elseif(empty($field['name'])){
			$json['warn'] = "订单名称为空";
		}elseif(sqlNum("buyCar"," khid = '$kehu[khid]' and name = '$field[name]' and id != '".$this->post['id']."' ") > 0){
			$json['warn'] = "同一个客户存在相同的订单名称";
		}elseif(empty($field['money'])){
			$json['warn'] = "合同金额为空";
		}elseif(preg_match(checkPrice,$field['money']) == 0){
			$json['warn'] = "合同金额格式有误";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "订单ID为空";
		}elseif(empty($buyCar['id'])){
			$field = array_merge($field,array("id" => $this->post['id'],"stid" => $kehu['stid'],"khid" => $kehu['khid'],"workFlow" => "草稿","time" => date("Y-m-d H:i:s")));
			$json['warn'] = insert(array("table" => "buyCar","field" => $field));
		}elseif($buyCar['workFlow'] != "草稿"){
			$json['warn'] = "只有处于“草稿”状态的订单才可以编辑";
		}else{
			$json['warn'] = update(array("table" => "buyCar","field" => $field,"where" => "id = '".$this->post['id']."'"));
		}
		if(in_array($json['warn'],array("新增成功","更新成功"))){
			//添加日志
			$type = mb_substr($json['warn'],0,2,'utf-8');
			$text = $this->control['name'].$type."了一笔订单：客户名称：".$kehu['companyName']."；订单名称：".$field['name'];
			logText(array("type" => "order","text" => $text));
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
}
?>